package com.sz.common.base.dao;

import java.io.Serializable;

/**
 * Function: 分页插件 <br>
 * Author: hpeng.wang <br>
 * Date: 2016-11-01 15:11:00
 */
public class Pagination implements Serializable {

    public static final int MAX_PAGE_SIZE = 2000;
    public static final int MIN_RESULT = 0;
    private int pageSize; // 每页显示记录数
    private int totalPage; // 总页数
    private int totalResult; // 总记录数
    private int pageIndex; // 当前页
    private int currentResult; // 当前记录起始索引

    public Pagination(int pageSize, int pageIndex) {
        this.pageSize = pageSize;
        this.pageIndex = pageIndex;
    }

    /**
     * 无参构造方法.
     */
    public Pagination() {
        this.pageSize = 20;
    }

    /**
     * 获取总页数（根据总记录数%每页显示记录数 计算得出）.
     *
     * @return 计算出来的总页数
     */
    public int getTotalPage() {
        if (totalResult % pageSize == 0) {
            totalPage = totalResult / pageSize;
        } else {
            totalPage = totalResult / pageSize + 1;
        }
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotalResult() {
        return totalResult;
    }

    public void setTotalResult(int totalResult) {
        this.totalResult = totalResult;
    }

    /**
     * 获取当前页.
     *
     * @return 返回当前页的索引编号
     */
    public int getCurrentPage() {
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
       /* if (pageIndex > getTotalPage()) {
            pageIndex = getTotalPage();
        }*/
        return pageIndex;
    }

    public void setCurrentPage(int currentPage) {
        this.pageIndex = currentPage;
    }

    public int getPageSize() {
        if (pageSize > MAX_PAGE_SIZE) {
            pageSize = MAX_PAGE_SIZE;
        }
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获取当前记录起始索引页(当前页*每页显示记录数).
     *
     * @return 当前记录的起始索引页
     */
    public int getCurrentResult() {
        currentResult = (getCurrentPage() - 1) * getPageSize();
        if (currentResult < MIN_RESULT) {
            currentResult = MIN_RESULT;
        }
        return currentResult;
    }

}
